home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1459.dms / var1459.adf / LowLevelGraphics / Example3.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  8KB  |  238 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example3.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example shows how to create a display that covers the entire  */
  21. /* display. This method is called "Overscan", and is primarly used in */
  22. /* video and graphics programs, but can also be used in games etc to  */
  23. /* make the display more interesting.                                 */
  24.  
  25. /* EXTRA INFORMATION                                                  */
  26. /* If you want your programs to work on both American (NTSC) and      */
  27. /* European (PAL) machines you must either:                           */
  28. /* 1. Not make the display taller than 200 lines. The program will    */
  29. /*    then run perfectly on both types of machines, BUT the European  */
  30. /*    user would be very annoyed since the last 56 lines could not    */
  31. /*    be used.                                                        */
  32. /* 2. Look at the GfxBase structure and see if the program is running */
  33. /*    on an American machine, set the height to max 200 lines, or if  */
  34. /*    the program is running on a European machine, set the height    */
  35. /*    to max 256 lines. (For interlaced displays: 400 or 512 lines)   */
  36. /*    Example:  if( GfxBase->DisplayFlags & NTSC )                    */
  37. /*                Height=200;                                         */
  38. /*              if( GfxBase->DisplayFlags & PAL )                     */
  39. /*                Height=256;                                         */
  40.  
  41.  
  42. #include <intuition/intuition.h>
  43. #include <graphics/gfxbase.h>
  44.  
  45.  
  46. #define WIDTH       352 /* Display 352 pixels wide. [Overscan]       */ 
  47. #define NTSC_HEIGHT 262 /* Display 262 lines high. [NTSC - Overscan] */
  48. #define PAL_HEIGHT  287 /* Display 287 lines high. [PAL - Overscan]  */
  49.  
  50. /* The ViewPort should be placed above and more to the    */
  51. /* left than what is normally used:                       */
  52. #define DXOFFSET -16 /* DxOffset -16 pixels.              */
  53. #define DYOFFSET -31 /* DyOffset -31 lines.               */
  54.  
  55. #define DEPTH      2 /* 2 BitPlanes should be used, gives four colours. */
  56. #define COLOURS    8 /* 2^2 = 4                                         */
  57.  
  58.  
  59. struct IntuitionBase *IntuitionBase;
  60. struct GfxBase *GfxBase;
  61.  
  62.  
  63. struct View my_view;
  64. struct View *my_old_view;
  65. struct ViewPort my_view_port;
  66. struct RasInfo my_ras_info;
  67. struct BitMap my_bit_map;
  68. struct RastPort my_rast_port;
  69.  
  70.  
  71. UWORD my_color_table[] =
  72. {
  73.   0x000, /* Colour 0, Black */
  74.   0xF00, /* Colour 1, Red   */
  75.   0x0F0, /* Colour 2, Green */
  76.   0x00F, /* Colour 3, Blue  */
  77. };
  78.  
  79.  
  80. SHORT height;  
  81.  
  82.  
  83. void clean_up();
  84. void main();
  85.  
  86.  
  87. void main()
  88. {
  89.   UWORD *pointer;
  90.   int loop;
  91.  
  92.  
  93.   /* Open the Intuition library: */
  94.   IntuitionBase = (struct IntuitionBase *)
  95.     OpenLibrary( "intuition.library", 0 );
  96.   if( !IntuitionBase )
  97.     clean_up( "Could NOT open the Intuition library!" );
  98.  
  99.   /* Open the Graphics library: */
  100.   GfxBase = (struct GfxBase *)
  101.     OpenLibrary( "graphics.library", 0 );
  102.   if( !GfxBase )
  103.     clean_up( "Could NOT open the Graphics library!" );
  104.  
  105.  
  106.   /* Check if the program is running on a PAL or NTSC machine: */
  107.   if( GfxBase->DisplayFlags & PAL )
  108.   {
  109.     height = PAL_HEIGHT;
  110.     printf( "You have an European (PAL) machine!\n" );
  111.   }
  112.   else
  113.   {
  114.     height = NTSC_HEIGHT;
  115.     printf( "You have an American (NTSC) machine!\n" );
  116.   }
  117.  
  118.  
  119.   /* Save the current View, so we can restore it later: */
  120.   my_old_view = GfxBase->ActiView;
  121.  
  122.  
  123.   /* 1. Prepare the View structure, and give it a pointer to */
  124.   /*    the first ViewPort:                                  */
  125.   InitView( &my_view );
  126.   my_view.ViewPort = &my_view_port;
  127.  
  128.  
  129.   /* 2. Prepare the ViewPort structure, and set some important values:   */
  130.   InitVPort( &my_view_port );
  131.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  132.   my_view_port.DHeight = height;       /* Set the height.               */
  133.   my_view_port.DxOffset = DXOFFSET;    /* Set the display X offset.     */
  134.   my_view_port.DyOffset = DYOFFSET;    /* Set the display Y offset.     */
  135.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  136.   my_view_port.Modes = NULL;           /* Low resolution.               */
  137.  
  138.  
  139.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  140.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  141.   if( my_view_port.ColorMap == NULL )
  142.     clean_up( "Could NOT get a ColorMap!" );
  143.  
  144.   /* Get a pointer to the colour map: */
  145.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  146.  
  147.   /* Set the colours: */
  148.   for( loop = 0; loop < COLOURS; loop++ )
  149.     *pointer++ = my_color_table[ loop ];
  150.  
  151.  
  152.   /* 4. Prepare the BitMap: */
  153.   InitBitMap( &my_bit_map, DEPTH, WIDTH, height );
  154.  
  155.   /* Allocate memory for the Raster: */ 
  156.   for( loop = 0; loop < DEPTH; loop++ )
  157.   {
  158.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, height );
  159.     if( my_bit_map.Planes[ loop ] == NULL )
  160.       clean_up( "Could NOT allocate enough memory for the raster!" );
  161.  
  162.     /* Clear the display memory with help of the Blitter: */
  163.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, height ), 0 );
  164.   }
  165.  
  166.   
  167.   /* 5. Prepare the RasInfo structure: */
  168.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  169.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  170.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  171.                                     /* of the display.                   */
  172.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  173.                                     /* RasInfo structure is necessary.   */
  174.  
  175.   /* 6. Create the display: */
  176.   MakeVPort( &my_view, &my_view_port );
  177.   MrgCop( &my_view );
  178.  
  179.  
  180.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  181.   InitRastPort( &my_rast_port );
  182.   my_rast_port.BitMap = &my_bit_map;
  183.   
  184.  
  185.   /* 8. Show the new View: */
  186.   LoadView( &my_view );
  187.  
  188.  
  189.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  190.   SetDrMd( &my_rast_port, JAM1 );
  191.   /* Draw 10000 pixels in seven different colours, randomly. */ 
  192.   for( loop = 0; loop < 10000; loop++ )
  193.   {
  194.     /* Set FgPen's colour (1-7, 0 used for the the background). */
  195.     SetAPen( &my_rast_port, rand() % (COLOURS-1) + 1 );
  196.     /* Write a pixel somewere on the display: */
  197.     WritePixel( &my_rast_port, rand() % WIDTH, rand() % height );
  198.   }
  199.  
  200.  
  201.   /* 9. Restore the old View: */
  202.   LoadView( my_old_view );
  203.  
  204.  
  205.   /* Free all allocated resources and leave. */
  206.   clean_up( "THE END" );
  207. }
  208.  
  209.  
  210. /* Returns all allocated resources: */
  211. void clean_up( message )
  212. STRPTR message;
  213. {
  214.   int loop;
  215.  
  216.   /* Free automatically allocated display structures: */
  217.   FreeVPortCopLists( &my_view_port );
  218.   FreeCprList( my_view.LOFCprList );
  219.   
  220.   /* Deallocate the display memory, BitPlane for BitPlane: */
  221.   for( loop = 0; loop < DEPTH; loop++ )
  222.     if( my_bit_map.Planes[ loop ] )
  223.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, height );
  224.  
  225.   /* Deallocate the ColorMap: */
  226.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  227.  
  228.   /* Close the Graphics library: */
  229.   if( GfxBase ) CloseLibrary( GfxBase );
  230.  
  231.   /* Close the Intuition library: */
  232.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  233.  
  234.   /* Print the message and leave: */
  235.   printf( "%s\n", message ); 
  236.   exit();
  237. }
  238.